home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0429.dms / q0429.adf / libray / libcommon / rotate.c < prev    next >
C/C++ Source or Header  |  1992-05-20  |  3KB  |  114 lines

  1. /*
  2.  * rotate.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  * 
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: rotate.c,v 4.0.1.1 91/09/29 15:35:38 cek Exp Locker: cek $
  17.  *
  18.  * $Log:    rotate.c,v $
  19.  * Revision 4.0.1.1  91/09/29  15:35:38  cek
  20.  * patch1: Added comments.
  21.  * 
  22.  * Revision 4.0  91/07/17  14:31:18  kolb
  23.  * Initial version.
  24.  * 
  25.  */
  26. #include "common.h"
  27. #include "rotate.h"
  28.  
  29. TransMethods *iRotateMethods;
  30. void RotationMatrix();
  31.  
  32. /*
  33.  * Create and return reference to Rotate structure.
  34.  */
  35. Rotate *
  36. RotateCreate()
  37. {
  38.     Rotate *res;
  39.  
  40.     res = (Rotate *)Malloc(sizeof(Rotate));
  41.     res->x = res->y = res->theta = 0.;
  42.     res->z = 1.;
  43.     return res;
  44. }
  45.  
  46. /*
  47.  * Return a pointer to collection of methods for the
  48.  * Rotate transformation.
  49.  */
  50. TransMethods *
  51. RotateMethods()
  52. {
  53.     if (iRotateMethods == (TransMethods *)NULL) {
  54.         iRotateMethods = (TransMethods *)Malloc(sizeof(TransMethods));
  55.         iRotateMethods->create = (TransCreateFunc *)RotateCreate;
  56.         iRotateMethods->propagate = RotatePropagate;
  57.     }
  58.     return iRotateMethods;    
  59. }
  60.  
  61. /*
  62.  * Given a Rotate structure and forward and inverse transformations,
  63.  * propagate the information in the Rotate structure to the
  64.  * transformations.
  65.  */
  66. void
  67. RotatePropagate(rotate, trans, itrans)
  68. Rotate *rotate;
  69. RSMatrix *trans, *itrans;
  70. {
  71.     Vector axis;
  72.  
  73.     RotationMatrix(rotate->x, rotate->y, rotate->z, deg2rad(rotate->theta), trans);
  74.     /*
  75.      * Build the inverse...
  76.      */
  77.     MatrixInvert(trans, itrans);
  78. }
  79.  
  80. /*
  81.  * Initialize a rotation matrix given an axis of rotation and an
  82.  * angle.  Right-handed rotation is applied.
  83.  */
  84. void
  85. RotationMatrix(x, y, z, theta, trans)
  86. Float x, y, z, theta;
  87. RSMatrix *trans;
  88. {
  89.     Float n1, n2, n3, sintheta, costheta;
  90.     Vector vector;
  91.  
  92.     MatrixInit(trans);
  93.     vector.x = x;
  94.     vector.y = y;
  95.     vector.z = z;
  96.  
  97.     if (VecNormalize(&vector) == 0.)
  98.         RLerror(RL_WARN, "Degenerate rotation axis.\n");
  99.  
  100.     sintheta = sin(theta);
  101.     costheta = cos(theta);
  102.  
  103.     n1 = vector.x; n2 = vector.y; n3 = vector.z;
  104.     trans->matrix[0][0] = (Float)(n1*n1 + (1. - n1*n1)*costheta);
  105.     trans->matrix[0][1] = (Float)(n1*n2*(1 - costheta) + n3*sintheta);
  106.     trans->matrix[0][2] = (Float)(n1*n3*(1 - costheta) - n2*sintheta);
  107.     trans->matrix[1][0] = (Float)(n1*n2*(1 - costheta) - n3*sintheta);
  108.     trans->matrix[1][1] = (Float)(n2*n2 + (1 - n2*n2)*costheta);
  109.     trans->matrix[1][2] = (Float)(n2*n3*(1 - costheta) + n1*sintheta);
  110.     trans->matrix[2][0] = (Float)(n1*n3*(1 - costheta) + n2*sintheta);
  111.     trans->matrix[2][1] = (Float)(n2*n3*(1 - costheta) - n1*sintheta);
  112.     trans->matrix[2][2] = (Float)(n3*n3 + (1 - n3*n3)*costheta);
  113. }
  114.